<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class m_news extends CI_Model{
	
	function __construct()
	{
		parent::__construct();
	} 
	function get_news_num(){
		
		return $this->db->count_all('com_news');
	}
	function get_news($num, $offset){
		$query = $this->db->get('com_news', $num, $offset);      
        return $query->result();   
	}
	function get_news_type(){
		return $this->db->query('select id,type_name  from `'.$this->db->dbprefix('com_news_type').'` ')->result_array();	
	}
	function get_news_type_name($type_id){
		return $this->db->query('select type_name  from `'.$this->db->dbprefix('com_news_type').'` where id='.$type_id)->row_array();	
	}
	function save_news($type_name,$title,$content,$attachment_title,$file_name,$is_common,$time,$pai){
		$save_news= $this->db->query('insert into `'.$this->db->dbprefix('com_news').'` set 
				`type_id`=\''.$type_name.'\',
				`title`=\''.$title.'\',
				`content`=\''.$content.'\',
				`content_url`="",
				`attachment_title`=\''.$attachment_title.'\',
				`attachment_file`=\''.$file_name.'\',
				`is_common`=\''.$is_common.'\',
				`add_time`=\''.$time.'\',
				`pai`=\''.$pai.'\'
		');
		$insert_id= $this->db->insert_id();
		$update_version=$this->db->query('update  `'.$this->db->dbprefix('com_news_type').'` set `version`=version+1 where `id`=\''.$type_name.'\'');	
		
		return  $insert_id;
	}
	function update_html_url($html_url,$id){
		return  $this->db->query('update  `'.$this->db->dbprefix('com_news').'` set `content_url`=\''.$html_url.'\' where `id`=\''.$id.'\'');	
		
	}
	function save_common_message($file_name,$message,$title){
	///	$message=json_encode($message);
		//echo 'insert into `'.$this->db->dbprefix(' u_message').'` (`member_id`,`from_member_id`,`type`,`message`,`title`,`mtime`,`isread`) values (0,0,4,\''.$message.'\',\''.$title.'\','.time().',0)';
		return $this->db->query( 'insert into '.$this->db->dbprefix('u_message').' set 
				`member_id`=0,
				`from_member_id`=0,
				`type`=4,
				`message`=\''.$message.'\',
				`title`=\''.$title.'\',
				`mtime`='.time().',
				`isread`=0
		');
	}
   function get_last_news(){
     	$last_news=$this->db->query('select n.type_id,n.title as news_summary,t.version,t.is_top as news_state from `'.$this->db->dbprefix('com_news').'` n,`'.$this->db->dbprefix('com_news_type').'` t where t.id=n.type_id group by n.type_id limit 5')->result_array();	
 	    $version=array();
 	    foreach ($last_news as $key=>$ln){
 	    	$version[$key]=$ln['version'];
 	    	unset($last_news[$key]['version']);
 	    }
 	  $return_value=array(
 	   'version'=>max($version),
 	    'info'=>$last_news	  
 	  );
 	  return $return_value;
   }
   function get_news_list($type_id){
	   $base_url = 'http://z.no4e.com/';
   	  	$news_list=$this->db->query('select id as news_id,title as news_title,add_time as news_time,content_url as news_content,attachment_title as news_accessory_tip, attachment_file as news_accessory from `'.$this->db->dbprefix('com_news').'` order by pai  desc ')->result_array();;	
 	    foreach ($news_list as $key=>$nl){
 	    	$news_list[$key]['news_time']=$nl['news_time'];
 	    	$news_list[$key]['news_content']=$base_url.$nl['news_content'];
 	    	$news_list[$key]['news_accessory']=$base_url.'static/news/attachment_file/'.$nl['news_accessory'];
 	    }
     	$version=$this->db->query('select version  from `'.$this->db->dbprefix('com_news_type').'` where id='.$type_id)->row_array();
 	    $return_value=array(
 	     'version'=>$version['version'],
 	     'type_id'=>$type_id,
 	     'info'=>$news_list  
 	  );
 	  return $return_value;
   }
	function get_news_ById($id){
		return $this->db->query('select * from  `'.$this->db->dbprefix('com_news').'` where `id`=\''.$id.'\' ')->row_array();

	}
	function update_news($type_name,$title,$content,$attachment_title,$file_name,$is_common,$id,$pai){
	    return $this->db->query('update  `'.$this->db->dbprefix('com_news').'` set `type_id`='.$type_name.',title=\''.$title.'\',content=\''.$content.'\' ,attachment_title=\''.$attachment_title.'\',attachment_file=\''.$file_name.'\',is_common='.$is_common.' ,pai='.$pai.' where `id`='.$id);	
	}
	function delete_news($id){
		return $this->db->query('delete from `'.$this->db->dbprefix('com_news').'`  where  id ='.$id);	
	}
	function batch_delete_news($ids){
		return $this->db->query('delete from `'.$this->db->dbprefix('com_news').'`  where  id in('.$ids.')');	
	}
}
?>